package org.xfy.ymcc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.xfy.common.util.AssertUtil;
import org.xfy.ymcc.config.AliPayConfig;
import org.xfy.ymcc.dto.PayOrderDto;
import org.xfy.ymcc.service.IPayOrderService;
import org.xfy.ymcc.domain.PayOrder;
import org.xfy.ymcc.query.PayOrderQuery;
import org.xfy.common.result.JsonResult;
import org.xfy.common.result.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/payOrder")
public class PayOrderController {

    @Autowired
    public IPayOrderService payOrderService;
    @Autowired
    private AliPayConfig aliPayConfig;

    /**
     * 支付跳转
     * @return
     */
    @PostMapping("/apply")
    public JsonResult apply(@RequestBody PayOrderDto payOrderDto){
        PayOrder one = payOrderService.getOne(new LambdaQueryWrapper<PayOrder>().eq(PayOrder::getOrderNo, payOrderDto.getOrderNo()));
        Integer payType = payOrderDto.getPayType();
        switch (payType) {
            case 1:
                String payUrl = aliPayConfig.toPay(one.getSubject(), one.getOrderNo(), one.getAmount().toString(), payOrderDto.getCallUrl());
                return JsonResult.success().setData(payUrl);
            case 2:
                //微信
                break;
            case 3:
                //银联
                break;
            default:
                break;
        }
        return JsonResult.error("支付异常");

    }

    @GetMapping("/checkPayOrder/{orderNo}")
    public JsonResult checkPayOrder(@PathVariable("orderNo")String orderNo){
        PayOrder one = payOrderService.getOne(new LambdaQueryWrapper<PayOrder>().eq(PayOrder::getOrderNo, orderNo));
        AssertUtil.isNotNull(one,"订单未生成");
        return JsonResult.success();
    }

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody PayOrder payOrder){
        if(payOrder.getId()!=null){
            payOrderService.updateById(payOrder);
        }else{
            payOrderService.save(payOrder);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        payOrderService.removeById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success().setData(payOrderService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.success().setData(payOrderService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody PayOrderQuery query){
        Page<PayOrder> page = new Page<PayOrder>(query.getPageNo(), query.getPageSize());
        page = payOrderService.page(page);
        return JsonResult.success().setData(new PageList<PayOrder>(page.getTotal(), page.getRecords()));
    }
}
